#include "DynamicPropertyValue.h"
#include <stdexcept>

using namespace std;


_BEGIN_BOARDGAME_NAMESPACE_


DynamicPropertyValue::DynamicPropertyValue( DynamicPropertyType type )
: m_type( type )
, m_boolValue( false )
, m_floatValue( 0.0f )
, m_intValue( 0 )
, m_stringValue( t("") )
{
}


DynamicPropertyType DynamicPropertyValue::GetType() const
{
	return m_type;
}


DynamicPropertyValue::operator bool() const
{
	this->CheckType( DPT_Boolean );
	return m_boolValue;
}


DynamicPropertyValue::operator int() const
{
	this->CheckType( DPT_Integer );
	return m_intValue;
}


DynamicPropertyValue::operator float() const
{
	this->CheckType( DPT_Float );
	return m_floatValue;
}


DynamicPropertyValue::operator const tchar *() const
{
	this->CheckType( DPT_String );
	return m_stringValue.c_str();
}


DynamicPropertyValue::operator const tstring() const
{
	this->CheckType( DPT_String );
	return m_stringValue;
}


DynamicPropertyValue & DynamicPropertyValue::operator=( bool value )
{
	this->CheckType( DPT_Boolean );
	m_boolValue = value;
	return *this;
}


DynamicPropertyValue & DynamicPropertyValue::operator=( int value )
{
	this->CheckType( DPT_Integer );
	m_intValue = value;
	return *this;
}


DynamicPropertyValue & DynamicPropertyValue::operator=( float value )
{
	this->CheckType( DPT_Float );
	m_floatValue = value;
	return *this;
}


DynamicPropertyValue & DynamicPropertyValue::operator=( const tchar * value )
{
	this->CheckType( DPT_String );
	m_stringValue = value;
	return *this;
}


DynamicPropertyValue & DynamicPropertyValue::operator=( const tstring & value )
{
	this->CheckType( DPT_String );
	m_stringValue = value;
	return *this;
}


tostream & operator<<( tostream & os, const DynamicPropertyValue & value )
{
	switch( value.GetType() )
	{
	case DPT_Boolean:
		os << value.m_boolValue;
		break;
	case DPT_Float:
		os << value.m_floatValue;
		break;
	case DPT_Integer:
		os << value.m_intValue;
		break;
	case DPT_String:
		os << value.m_stringValue;
		break;
	}

	return os;
}


void DynamicPropertyValue::CheckType( DynamicPropertyType type ) const
{
	if( m_type != type )
	{
		throw invalid_argument( "Type mismatch." );
	}
}


_END_BOARDGAME_NAMESPACE_